package com.fjb.controller.product;


import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.JsonTotalResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.enums.product.ProductStatus;
import com.fjb.common.enums.user.UserType;
import com.fjb.common.global.GlobalConfig;
import com.fjb.controller.base.BaseController;
import com.fjb.pojo.login.LoginUserInfo;
import com.fjb.pojo.product.ProductAttribute;
import com.fjb.pojo.product.ProductAttributeSku;
import com.fjb.pojo.product.ProductFiles;
import com.fjb.pojo.product.ProductInfo;
import com.fjb.pojo.product.query.ProductInfoQuery;
import com.fjb.pojo.product.vo.ProductInfoPageVo;
import com.fjb.pojo.user.SysUser;
import com.fjb.service.product.ProductInfoService;

/**
 * <p>
 * 产品信息 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Controller
@RequestMapping("/productInfo")
public class ProductInfoController extends BaseController{
	
	@Autowired
	private ProductInfoService productInfoService;
	
	/**
	 * @Description:显示分页列表
	 * @param request
	 * @return
	 * ModelAndView
	 * @exception:
	 * @author: hemiao
	 * @time:2019年12月28日 下午5:58:26
	 */
	@RequestMapping(value="/showPageList")
    public ModelAndView showPageList(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();	
    	mv.setViewName("/product/product_info_list");
    	mv.addObject(FILE_SERVICE_LOCATION, GlobalConfig.FILE_SERVICE_LOCATION);
    	return mv;
    }
	
	/**
	 * @Description:分页列表查询
	 * @param request
	 * @param infoQuery
	 * @param pageQuery
	 * @return
	 * JsonTotalResult<List<ProductInfoPageVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月12日 下午7:12:32
	 */
	@RequestMapping(value = "/selectPageList")
    @ResponseBody
    public JsonTotalResult<List<ProductInfoPageVo>> selectPageList(HttpServletRequest request,ProductInfoQuery infoQuery,
    		PageQuery pageQuery){
		String tagQuery = request.getParameter("tagQuery");
		if(StringUtils.isNotBlank(tagQuery)) {
			// 新品 
			if(tagQuery.equals("1")) {
				infoQuery.setIsNew(1);
			}
			// 热卖
			if(tagQuery.equals("2")) {
				infoQuery.setIsHot(1);
			}
		}
		// 排序查询
		String countQuery = request.getParameter("countQuery");
		if(StringUtils.isNotBlank(countQuery)) {
			// 浏览次数
			if(countQuery.equals("1")) {
				infoQuery.setOrderByStr("pi.view_count");
				infoQuery.setOrderByTypeStr("DESC");
			}
			// 收藏次数
			if(countQuery.equals("2")) {
				infoQuery.setOrderByStr("pi.collect_count");
				infoQuery.setOrderByTypeStr("DESC");
			}
			// 超赞次数
			if(countQuery.equals("3")) {
				infoQuery.setOrderByStr("pi.praise_count");
				infoQuery.setOrderByTypeStr("DESC");
			}
			// 想要次数
			if(countQuery.equals("4")) {
				infoQuery.setOrderByStr("pi.want_count");
				infoQuery.setOrderByTypeStr("DESC");
			}
		}else {
			infoQuery.setOrderByStr("pi.create_time");
			infoQuery.setOrderByTypeStr("DESC");
		}
        return productInfoService.selectPageList(infoQuery,pageQuery);
    }
	
	/**
	 * @Description:添加
	 * @param request
	 * @param categoryId
	 * @param brandId
	 * @param sort
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月12日 下午2:02:37
	 */
	@RequestMapping(value = "/addInfo")
    @ResponseBody
    public JsonResult<Integer> addInfo(HttpServletRequest request,Integer categoryId,Integer brandId,Integer productSkuCount
    		,BigDecimal originalPrice,BigDecimal sellingPrice,Integer deliveryModeId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(categoryId==null) {	
			httpCode.setMsg("分类id为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(brandId==null) {	
			httpCode.setMsg("品牌id为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productName = request.getParameter("productName");
		if(StringUtils.isBlank(productName)) {
			httpCode.setMsg("产品名称不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productUnit = request.getParameter("productUnit");
		if(StringUtils.isBlank(productUnit)) {
			httpCode.setMsg("产品单位不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(deliveryModeId==null) {
			httpCode.setMsg("交货方式不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		
		String productTitle = request.getParameter("productTitle");
			
		// 属性
		String attrInfoListStr = request.getParameter("attrInfoListStr");
		if(StringUtils.isBlank(attrInfoListStr)) {
			httpCode.setMsg("属性不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		// 属性sku
		String attrSkuListStr = request.getParameter("attrSkuListStr");
		if(StringUtils.isBlank(attrSkuListStr)) {
			httpCode.setMsg("sku不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		
		List<ProductAttribute> attributeLiat = JSON.parseArray(attrInfoListStr, ProductAttribute.class);
		if(attributeLiat.size()==0) {
			httpCode.setMsg("属性解析失败,请联系管理员");
			return new JsonResult<Integer>(null, httpCode);
		}
		
		List<ProductAttributeSku> attributeSkuList = JSON.parseArray(attrSkuListStr, ProductAttributeSku.class);
		if(attributeSkuList.size()==0) {
			httpCode.setMsg("sku解析失败,请联系管理员");
			return new JsonResult<Integer>(null, httpCode);
		}
		
		// 产品文件	productFilesListStr
		List<ProductFiles> filesList = new ArrayList<ProductFiles>();
		String productFilesListStr = request.getParameter("productFilesListStr");
		if(StringUtils.isNoneBlank(productFilesListStr)) {
			filesList = JSON.parseArray(productFilesListStr,ProductFiles.class);
		}
		
		// 详情
		String productDetails = request.getParameter("editorValue");
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer mainUserId = getMainUserId(sysUser);
		Integer userId = sysUser.getId();
			
		ProductInfo product = new ProductInfo();
		product.setBrandId(brandId);
		product.setCategoryId(categoryId);
		product.setProductName(productName);
		if(StringUtils.isNoneBlank(productTitle)) {
			product.setProductTitle(productTitle);
		}
		//product.setProductSkuCount(productSkuCount);
		product.setProductCount(productSkuCount);
		product.setOriginalPrice(originalPrice);
		//product.setSellingPrice(sellingPrice);
		product.setCurrentPrice(sellingPrice);
		product.setProductDetails(productDetails);
		product.setMainUserId(mainUserId);
		product.setCreateUserId(userId);
		product.setProductUnit(productUnit);
		product.setDeliveryModeId(deliveryModeId);
		return productInfoService.addInfo(product,attributeLiat,attributeSkuList,filesList);
	}
	
	/**
	 * @Description:修改
	 * @param request
	 * @param productInfoId
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月12日 下午7:20:51
	 */
	@RequestMapping(value = "/updateInfo")
    @ResponseBody
    public JsonResult<Boolean> updateInfo(HttpServletRequest request,Integer productInfoId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		
		
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer mainUserId = getMainUserId(sysUser);
		Integer userId = sysUser.getId();
		
		ProductInfo productInfo = new ProductInfo();
		Boolean b = productInfoService.updateInfo(productInfo);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:根据id查询
	 * @param request
	 * @param productInfoId
	 * @return
	 * JsonResult<ProductInfo>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月12日 下午7:20:40
	 */
	@RequestMapping(value = "/getById")
    @ResponseBody
    public JsonResult<ProductInfo> getById(HttpServletRequest request,Integer productInfoId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(productInfoId==null) {	
			return new JsonResult<ProductInfo>(null, httpCode);
		}		
		ProductInfo category = productInfoService.getById(productInfoId);
		return new JsonResult<ProductInfo>(category, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:添加页面
	 * @param request
	 * @return
	 * ModelAndView
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月18日 下午5:36:22
	 */
	@RequestMapping(value="/showAddInfo")
    public ModelAndView showAddInfo(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();	
    	mv.setViewName("/product/add_product_ifno");	
    	mv.addObject(FILE_SERVICE_LOCATION, GlobalConfig.FILE_SERVICE_LOCATION);
    	return mv;
    }
	
	/**
	 * @Description:修改新品
	 * @param request
	 * @param productInfoId
	 * @param switchs
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月3日 下午11:00:18
	 */
	@RequestMapping(value = "/updateIsNew")
    @ResponseBody
    public JsonResult<Boolean> updateIsNew(HttpServletRequest request,Integer productInfoId,Boolean switchs){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(productInfoId==null||switchs==null) {	
			return new JsonResult<Boolean>(null, httpCode);
		}		
		
		ProductInfo productInfo = new ProductInfo();
		productInfo.setId(productInfoId);
		if(switchs==true) {
			productInfo.setIsNew(1);
		}else {
			productInfo.setIsNew(0);
		}
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer userId = sysUser.getId();
		productInfo.setUpdateTime(LocalDateTime.now());
		productInfo.setUpdateUserId(userId);
		Boolean b = productInfoService.updateById(productInfo);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:修改热卖
	 * @param request
	 * @param productInfoId
	 * @param switchs
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月3日 下午11:00:14
	 */
	@RequestMapping(value = "/updateIsHot")
    @ResponseBody
    public JsonResult<Boolean> updateIsHot(HttpServletRequest request,Integer productInfoId,Boolean switchs){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(productInfoId==null||switchs==null) {	
			return new JsonResult<Boolean>(null, httpCode);
		}		
		ProductInfo productInfo = new ProductInfo();
		productInfo.setId(productInfoId);
		if(switchs==true) {
			productInfo.setIsHot(1);
		}else {	
			productInfo.setIsHot(0);
		}
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer userId = sysUser.getId();
		productInfo.setUpdateTime(LocalDateTime.now());
		productInfo.setUpdateUserId(userId);
		Boolean b = productInfoService.updateById(productInfo);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:修改产品状态
	 * @param request
	 * @param productInfoId
	 * @param switchs
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月3日 下午11:39:34
	 */
	@RequestMapping(value = "/updateStatus")
    @ResponseBody
    public JsonResult<Boolean> updateStatus(HttpServletRequest request,Integer productInfoId,Integer productStatus){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(productInfoId==null||productStatus==null) {	
			return new JsonResult<Boolean>(null, httpCode);
		}
		ProductInfo oldProductInfo = productInfoService.getById(productInfoId);
		if(oldProductInfo==null) {
			httpCode.setMsg("产品信息不存在");
			return new JsonResult<Boolean>(null, httpCode);
		}
		LoginUserInfo userInfo = getUserInfo(request);
		SysUser sysUser = userInfo.getSysUser();
		Integer userId = sysUser.getId();
		Integer mainUserId = getMainUserId(sysUser);
		Integer userType = sysUser.getUserType();
		Integer oldProductStatus = oldProductInfo.getProductStatus();
		if(oldProductStatus.equals(ProductStatus.STATUS_3.getCode())&&!userType.equals(UserType.TYPE_1000.getCode())) {
			httpCode.setMsg("请联系管理员,产品信息违法");
			return new JsonResult<Boolean>(null, httpCode);
		}
		if(!userType.equals(UserType.TYPE_1000.getCode())) {
			Integer oldMainUserId = oldProductInfo.getMainUserId();
			if(!oldMainUserId.equals(mainUserId)) {
				return new JsonResult<Boolean>(null, HttpCode.NO_OPERATION_PERMISSION);
			}
		}
		ProductInfo productInfo = new ProductInfo();
		productInfo.setId(productInfoId);
		productInfo.setProductStatus(productStatus);
		productInfo.setUpdateTime(LocalDateTime.now());
		productInfo.setUpdateUserId(userId);
		Boolean b = productInfoService.updateById(productInfo);
		return new JsonResult<Boolean>(b, HttpCode.SUCCESS);
	}
}
